package com.sr.config;


import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
// 驱动缓存工作
@EnableCaching
public class RedisConfig {
    @Bean("redisPoolConfig")
    public JedisPoolConfig poolConfig() {
        JedisPoolConfig poolCfg = new JedisPoolConfig();
        // 最大空闲数
        poolCfg.setMaxIdle(50);
        // 最大连接数
        poolCfg.setMaxTotal(100);
        // 最大等待毫秒数
        poolCfg.setMaxWaitMillis(20000);
        return poolCfg;
    }
    /**
     * 创建Jedis连接工厂
     * @param jedisPoolConfig
     * @return 连接工厂
     */
    @Bean("redisConnectionFactory")
    public RedisConnectionFactory redisConnectionFactory(@Autowired JedisPoolConfig jedisPoolConfig) {
        // 独立Jedis配置
        RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
        // 设置Redis服务器
        rsc.setHostName("192.168.80.130");
        // 如需要密码，设置密码
        rsc.setPassword("abcdefg");
        // 端口
        rsc.setPort(6379);
        // 获得默认的连接池构造器
        JedisClientConfigurationBuilder jpcb = JedisClientConfiguration.builder();
        // 设置Redis连接池
        jpcb.usePooling().poolConfig(jedisPoolConfig);
        // 获取构建器
        JedisClientConfiguration jedisClientConfiguration = jpcb.build();
        // 创建连接工厂
        return new JedisConnectionFactory(rsc, jedisClientConfiguration);
    }
    /**
     * 创建RedisTemplate
     * @param connectionFactory Redis连接工厂
     * @return RedisTemplate对象
     */
    @Bean("redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory connectionFactory) {
        // 创建RedisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 字符串和JDK序列化器
        RedisSerializer<String> strSerializer = RedisSerializer.string();
        RedisSerializer<Object> jdkSerializer = RedisSerializer.java();
        // 设置键值序列化器
        redisTemplate.setKeySerializer(strSerializer);
        redisTemplate.setValueSerializer(jdkSerializer);
        // 设置哈希字段和值序列化器
        redisTemplate.setHashKeySerializer(strSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializer);
        // 给RedisTemplate设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
    /**
     * 创建StringRedisTemplate
     * @param connectionFactory 连接工厂
     * @return StringRedisTemplate对象
     */
    @Bean("stringRedisTemplate")
    public StringRedisTemplate  stringRedisTemplate(@Autowired RedisConnectionFactory connectionFactory) {
        // 创建StringRedisTemplate对象
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        // 设置连接工厂
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        return stringRedisTemplate;
    }

//	@Bean(name = "redisCacheManager")
//  public CacheManager initRedisCacheManager(@Autowired RedisConnectionFactory redisConnectionFactory) {
//        // 获取Redis缓存默认配置
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        // 构建Redis缓存管理器
//        RedisCacheManager cacheManager = RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
//                // 定义缓存管理器名称和配置，这样可以后续进行引用
//                .withCacheConfiguration("redisCacheManager", config)
//                .build();
//        return cacheManager;
//    }
    @Bean(name = "redisCacheManager")
    public CacheManager initRedisCacheManager(@Autowired RedisConnectionFactory redisConnectionFactory) {
        // 创建两个序列化器对
        SerializationPair<String> strSerializer = SerializationPair.fromSerializer(RedisSerializer.string());
        SerializationPair<Object> jdkSerializer = SerializationPair.fromSerializer(RedisSerializer.java());
        RedisCacheConfiguration config = RedisCacheConfiguration
                // 获取默认配置
                .defaultCacheConfig()
                // 设置超时时间
                .entryTtl(Duration.ofMinutes(30L))
                // 禁用前缀
                .disableKeyPrefix()
                // 自定义前缀
                // .prefixKeysWith("prefix")
                // 设置key序列化器
                .serializeKeysWith(strSerializer)
                // 设置value序列化器
                .serializeValuesWith(jdkSerializer)
                // 不缓冲空值
                .disableCachingNullValues();
        // 构建Redis缓存管理器
        RedisCacheManager cacheManager = RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
                        // 定义缓存管理器名称和配置，这样可以后续进行引用
                        .withCacheConfiguration("redisCacheManager", config)
                        .build();
        return cacheManager;
    }
}
